using System;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;

namespace DE.Sdc.SharePoint.Common.Deployment
{
    [Serializable]
    [XmlType(AnonymousType = true, Namespace = "http://sdc/2009/07/Deployment/1.0/ContentStructure")]
    [XmlRoot(Namespace = "http://sdc/2009/07/Deployment/1.0/ContentStructure", IsNullable = false)]
    public class Navigation 
    {
        private bool _inheritNavigation;

        public Navigation()
        {
            NavigationItems = new HierarchicalList<NavigationItem, NavigationItem>(null);
        }

        /// <remarks/>
        [XmlAttribute]
        public bool IncludePages { get; set; }

        /// <remarks/>
        [XmlAttribute]
        public bool IncludeSubSites { get; set; }

        /// <remarks/>
        [XmlAttribute]
        public bool IncludeHeadings { get; set; }

        /// <remarks/>
        [XmlAttribute]
        public bool InheritNavigation
        {
            get { return InheritNavigationSpecified ? _inheritNavigation : true; }
            set
            {
                _inheritNavigation = value;
                InheritNavigationSpecified = true;
            }
        }

        /// <remarks/>
        [XmlIgnore]
        public bool InheritNavigationSpecified { get; set; }
        
        /// <remarks/>
        [XmlAttribute]
        public bool IncludeAuthoredLinks { get; set; }

        /// <remarks/>
        [XmlAttribute]
        public NavigationSortMethodType SortMethod { get; set; }

        /// <remarks/>
        [XmlElement("NavigationItem", typeof(NavigationItem))]
        public HierarchicalList<NavigationItem, NavigationItem> NavigationItems { get; set; }

        /// <summary>
        /// Changes/Updates the Navigation
        /// </summary>
        /// <param name="navigationDefinition">Xml object for a navigation object</param>
        /// <param name="navigation">A sharepoint navigation object</param>
        protected internal static void ModifyNavigation(Navigation navigationDefinition, SPNavigation navigation)
        {
            try
            {
                SPWeb web = navigation.Web;

                BaseItem<NavigationItem>.ModifyProperty("__IncludeSubSitesInNavigation", navigationDefinition.IncludeSubSites.ToString(), false,
                               web.AllProperties);
                BaseItem<NavigationItem>.ModifyProperty("__IncludePagesInNavigation", navigationDefinition.IncludePages.ToString(), false,
                               web.AllProperties);
                BaseItem<NavigationItem>.ModifyProperty("__IncludeAuthoredLinksInNavigation", navigationDefinition.IncludeAuthoredLinks.ToString(), false,
                               web.AllProperties);
                BaseItem<NavigationItem>.ModifyProperty("__IncludeHeadingsInNavigation", navigationDefinition.IncludeHeadings.ToString(), false,
                               web.AllProperties);
                BaseItem<NavigationItem>.ModifyProperty("__InheritCurrentNavigation", navigationDefinition.InheritNavigation.ToString(), false,
                               web.AllProperties);

                if (!web.AllProperties.ContainsKey("__NavigationOrderingMethod"))
                {
                    BaseItem<NavigationItem>.ModifyProperty("__NavigationOrderingMethod", (int) navigationDefinition.SortMethod, false,
                                   web.AllProperties);
                }

                var parentNavigationItem = navigation.QuickLaunch.Parent;

                //if (navigationDefinition.SortMethod == NavigationSortMethodType.Manually)
                //{
                //    for (int index = 0; index < web.Webs.Count; index++)
                //    {
                //        SPWeb subWeb = web.Webs[index];

                //        var webNavigationNode = GetNavigationItem(subWeb.ServerRelativeUrl, parentNavigationItem);

                //        if (webNavigationNode == null)
                //        {
                //            CreateNavigationItem(new NavigationItem { Title = subWeb.Title, Url = subWeb.ServerRelativeUrl },
                //                                 parentNavigationItem, subWeb);
                //        }
                //    }
                //}

                if (navigationDefinition.NavigationItems != null)
                {
                    for (int index = 0; index < navigationDefinition.NavigationItems.Count; index++)
                    {
                        NavigationItem navigationItemDefinition = navigationDefinition.NavigationItems[index];
                        var navigationItem = NavigationItem.GetNavigationItem(navigationItemDefinition, navigation);
                        NavigationItem.ImportNavigationItem(navigationItemDefinition, navigationItem, parentNavigationItem);
                    }
                }

                web.Update();
            }
            catch (Exception)
            {
                {
                }
                throw;
            }
        }
    }
}